<template>
  <vue-modal
    @submitData="assignRole"
    modalTitle="分配角色"
    modalName="assignRoleModal"
    modalWidth="600"
    ref="assignRoleModal">
    <div slot="content" class="form-box">
      <div class="item">
        用户名：{{entity.username}}
      </div>
      <div class="item">
        请选择该用户的权限：
        <el-radio-group v-model="entity.roleId" :disabled="isRoot" class="radio-role-id">
          <el-radio
            v-for="(item, index) in roleData"
            :key="item.id"
            :label="item.id"
            class="radio-item">{{item.roleName}}</el-radio>
        </el-radio-group>
      </div>
    </div>
  </vue-modal>
</template>

<script>

  import VueModal from 'components/common/modal/VueModal';

  import {getAllRoles} from "../../../network/viewsRequest/role";
  import {assignRole} from "../../../network/viewsRequest/user";

  export default {
    name: "AssignRoleTemplate",
    data() {
      return {
        entity: {
          username: '',
          roleId: '',
        },
        roleData: [],
        isRoot: false, // 当本用户是root时，不允许修改角色
      }
    },
    props: {
      userDataFromParent: {
        type: Object,
        default: function() {
          return {};
        }
      }
    },
    components: {
      VueModal
    },
    watch: {
      "userDataFromParent.username": function(newValue) {
        if (newValue) {
          this.entity.username = newValue;
        }
      },
      "userDataFromParent.roleId": function(newValue) {
        if (newValue) {
          this.entity.roleId = newValue;
          if (newValue != 1) {
            this.isRoot = false;
          } else {
            this.isRoot = true;
          }
        }
      }
    },
    methods: {
      show() {
        this.$refs.assignRoleModal.show();
      },
      close() {
        this.$refs.assignRoleModal.closeModal();
      },
      loadRoleData() {
        getAllRoles().then(res => {
          if (res.code == 1) {
            this.roleData = res.data;
          } else {
            this.roleData = [];
          }
        })
      },
      assignRole() {

        this.$confirm(`确定要给用户${this.entity.username}进行权限分配?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          assignRole(this.entity).then(res => {
            if (res.code == 1) {
              this.$message.success("权限分配成功！");
              this.close();
              this.$emit("reloadTable");
            } else {
              this.$message.error(res.message);
            }
          })
        }).catch(() => {
          this.$message.info("操作已经被取消");
        })

      }
    },
    mounted() {
      this.loadRoleData();
    }
  }
</script>

<style scoped>
  .form-box {
    padding-left: 20px;
    padding-right: 20px;
  }
  .item {
    font-size: 18px;
    font-weight: bold;
    margin-bottom: 30px;
  }
  .item:last-child {
    margin-bottom: 0;
  }
  .radio-role-id {
    margin-top: 10px;
    display: flex;
    justify-content: left;
    flex-wrap: wrap;
  }
  .radio-role-id .radio-item {
    margin-top: 10px;
  }
</style>